/*
 * @lc app=leetcode.cn id=133 lang=cpp
 * @lcpr version=30204
 *
 * [133] 克隆图
 */


// @lcpr-template-start
using namespace std;
#include <algorithm>
#include <array>
#include <bitset>
#include <climits>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <queue>
#include <stack>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
// @lcpr-template-end
// @lc code=start
/*
// Definition for a Node.
class Node {
public:
    int val;
    vector<Node*> neighbors;
    Node() {
        val = 0;
        neighbors = vector<Node*>();
    }
    Node(int _val) {
        val = _val;
        neighbors = vector<Node*>();
    }
    Node(int _val, vector<Node*> _neighbors) {
        val = _val;
        neighbors = _neighbors;
    }
};
*/

class Solution {
private:
    unordered_map<Node*, Node*> visited; // 原节点 -> 克隆节点

    Node* dfsClone(Node* node) {
        if (!node) return nullptr;
        if (visited.count(node)) return visited[node]; // 已克隆，直接返回

        // 创建克隆节点
        Node* clone = new Node(node->val);
        visited[node] = clone;

        // 递归克隆邻居
        for (Node* neighbor : node->neighbors) {
            clone->neighbors.push_back(dfsClone(neighbor));
        }
        return clone;
    }

public:
    Node* cloneGraph(Node* node) {
        return dfsClone(node);
    }
};
// @lc code=end



/*
// @lcpr case=start
// [[2,4],[1,3],[2,4],[1,3]]\n
// @lcpr case=end

// @lcpr case=start
// [[]]\n
// @lcpr case=end

// @lcpr case=start
// []\n
// @lcpr case=end

 */

